package com.lepu.app.fun.my;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.app.utils.LoginUtil;
import com.core.lib.adapter.FragmentPageAdapter;
import com.core.lib.application.BaseFragmentActivity;
import com.eyzhs.app.R;
import com.lepu.app.fun.my.fragment.FragmentMyHotPhoto;
import com.lepu.app.fun.my.fragment.FragmentMyTopic;

public class MyPubContentActivity extends BaseFragmentActivity {

	private ViewPager mViewPager = null;
	private RelativeLayout mTabLayoutOne = null;
	private RelativeLayout mTabLayoutTwo = null;

	private View mTopLine1;
	private View mTopLine2;
	private int mCurrentTab = 0;

	private static MyPubContentActivity mInstance = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_pub_content_activity);

		mInstance = this;
		init();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();

		mInstance = null;
	}

	public static MyPubContentActivity getInstance() {
		return mInstance;
	}

	/**
	 * 添加页面动画，需要重写这个方法，
	 */
	@Override
	public void onBackPressed() {
		finish(true);
	}

	/**
	 * 初始化方法
	 */
	private void init() {

		// top bar
		LinearLayout backLayout = (LinearLayout) findViewById(R.id.backLayout);
		backLayout.setOnClickListener(mViewClickListener);

		// tab
		mTabLayoutOne = (RelativeLayout) findViewById(R.id.tabLayout1);
		mTabLayoutTwo = (RelativeLayout) findViewById(R.id.tabLayout2);

		mTabLayoutOne.setOnClickListener(mViewClickListener);
		mTabLayoutTwo.setOnClickListener(mViewClickListener);

		mTopLine1 = (View)findViewById(R.id.tabLine1);
		mTopLine2 = (View)findViewById(R.id.tabLine2);
		
		// content
		ArrayList<Fragment> fragmentList = new ArrayList<Fragment>();
		FragmentMyTopic tabOne = new FragmentMyTopic();
		FragmentMyHotPhoto tabTwo = new FragmentMyHotPhoto();

		Bundle data1 = new Bundle();
		data1.putString("fromContext", "MyPubContentActivity");
		data1.putString("targetUserID", LoginUtil.getUserId());
		tabOne.setArguments(data1);
		
		Bundle data2 = new Bundle();
		data2.putString("fromContext", "MyPubContentActivity");
		data2.putString("targetUserID", LoginUtil.getUserId());
		tabTwo.setArguments(data2);
		
		fragmentList.add(tabOne);
		fragmentList.add(tabTwo);

		mViewPager = (ViewPager) findViewById(R.id.myViewPager);
		mViewPager.setAdapter(new FragmentPageAdapter(this
				.getSupportFragmentManager(), fragmentList, null, null));
		mViewPager.setOnPageChangeListener(new MyViewChangeListener());
		
		mViewPager.setCurrentItem(0);
		updateTabLine(0);
	}

	View.OnClickListener mViewClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.tabLayout1:
				if (mCurrentTab == 0)
					return;

				mViewPager.setCurrentItem(0);
				updateTabLine(0);
				break;
			case R.id.tabLayout2:

				if (mCurrentTab == 1)
					return;

				mViewPager.setCurrentItem(1);
				updateTabLine(1);
				break;
			case R.id.backLayout:

				finish(true);
				break;
			default:
				break;
			}
		}
	};

	private void updateTabLine(int currentIndex) {
		if (currentIndex == 0) {
			mTabLayoutOne.setSelected(true);
			mTabLayoutTwo.setSelected(false);
			mTopLine1.setVisibility(View.VISIBLE);
			mTopLine2.setVisibility(View.INVISIBLE);
		} else {
			mTabLayoutOne.setSelected(false);
			mTabLayoutTwo.setSelected(true);
			mTopLine1.setVisibility(View.INVISIBLE);
			mTopLine2.setVisibility(View.VISIBLE);
		}
	}

	class MyViewChangeListener implements OnPageChangeListener {

		@Override
		public void onPageScrollStateChanged(int arg0) {

		}

		@Override
		public void onPageScrolled(int position, float positionOffset, int arg2) {

		}

		@Override
		public void onPageSelected(int index) {
			mCurrentTab = index;
			mViewPager.setCurrentItem(index);
			updateTabLine(index);
		}
	}

}
